package com.shenglue.ssi.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shenglue.ssi.domain.Dealer;
import com.shenglue.ssi.service.DealerService;

public class DealerAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private Dealer dealer;     // 对应的 Bean
	private int dealerId;    // 删除/修改 的 记录之Id
	private DealerService dealerService;  // 依赖的 Service

	public DealerService getDealerService() {
		return dealerService;
	}

	public void setDealerService(DealerService dealerService) {
		this.dealerService = dealerService;
	}

	public Dealer getDealer() {
		return dealer;
	}

	public void setDealer(Dealer dealer) {
		this.dealer = dealer;
	}

	public int getDealerId() {
		return dealerId;
	}

	public void setDealerId(int dealerId) {
		this.dealerId = dealerId;
	}

	public DealerAction() {
	}


	/**
	 * 查询所有经销商信息方法
	 */
	public String queryAll() throws Exception {
		List<Dealer> list = dealerService.selectAllDealer();
		if(list!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute( "dealerList", list );
		}else{
			// 不能认为是错误，因为一开始是没有数据的  return INPUT;
		}
		return SUCCESS;
	}

	/**
	 * 增加经销商记录方法
	 */
	public String addNew() throws Exception {
		boolean flag = dealerService.saveDealer( dealer );
		if(flag==true){
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 删除经销商记录方法
	 */
	public String delete() throws Exception {

		// 获取request对象
		boolean flag = dealerService.deleteDealer( dealerId );
		if( flag == true ) {
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 修改经销商记录方法
	 */
	public String update() throws Exception {

		boolean flag = dealerService.updateDealer( dealer );
		if( flag == true ) {
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
}
